package empleados.ventana.componentes.interfaz;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.Color;
import java.awt.Component;

import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.border.*;
import javax.swing.table.TableColumnModel;
import javax.swing.Icon;
import javax.swing.UIManager;
import javax.swing.Action;

import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableCellEditor;
import javax.swing.AbstractCellEditor;


public class BotonEnTabla extends AbstractCellEditor implements TableCellRenderer, TableCellEditor, ActionListener, MouseListener{

    private JTable table;
    private Action action;
    private Border originalBorder;
    private Border focusBorder;

    private JButton renderButton;
    private JButton editButton;
    private Object editorValue;
    private boolean isButtonColumnEditor;
    
    public BotonEnTabla(JTable table, int column, Action action, String texto){
        renderButton = new JButton(texto);
        editButton = new JButton(texto);
        editButton.setFocusPainted( false );
        editButton.addActionListener( this );
        originalBorder = editButton.getBorder();
        setFocusBorder( new LineBorder(Color.BLUE) );
        
        this.table = table;
        this.action = action;
        
        TableColumnModel columnModel = table.getColumnModel();
        columnModel.getColumn(column).setCellRenderer(this);
        columnModel.getColumn(column).setCellEditor(this);
        table.addMouseListener(this);
    }

    public Border getFocusBorder(){
        return focusBorder;
    }

    public void setFocusBorder(Border focusBorder){
        this.focusBorder = focusBorder;
        editButton.setBorder( focusBorder );
    }

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column){
        if (value == null){
            editButton.setText( "" );
            editButton.setIcon( null );
        }else if (value instanceof Icon){
            editButton.setText( "" );
            editButton.setIcon( (Icon)value );
        }else{
            editButton.setText( value.toString() );
            editButton.setIcon( null );
        }

        this.editorValue = value;
        return editButton;
    }

    @Override
    public Object getCellEditorValue(){
        return editorValue;
    }

//
//  Implement TableCellRenderer interface
//
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column){
        if (isSelected){
            renderButton.setForeground(table.getSelectionForeground());
            renderButton.setBackground(table.getSelectionBackground());
        }else{
            renderButton.setForeground(table.getForeground());
            renderButton.setBackground(UIManager.getColor("Button.background"));
        }

        if (hasFocus){
            renderButton.setBorder( focusBorder );
        }else{
            renderButton.setBorder( originalBorder );
        }

        return renderButton;
    }
    
    public void actionPerformed(ActionEvent e){
        int row = table.convertRowIndexToModel(table.getEditingRow());
        int column = table.convertColumnIndexToModel(table.getEditingColumn());
        fireEditingStopped();
        ActionEvent event = new ActionEvent( table,ActionEvent.ACTION_PERFORMED,""+row);
        action.actionPerformed(event);
    }

    public void mousePressed(MouseEvent e){
        if (table.isEditing() && table.getCellEditor() == this)
            isButtonColumnEditor = true;
    }
    
    public void mouseReleased(MouseEvent e){
        if (isButtonColumnEditor && table.isEditing())
            table.getCellEditor().stopCellEditing();
            isButtonColumnEditor = false;
    }
    
    public void mouseClicked(MouseEvent e){}
    public void mouseEntered(MouseEvent e){}
    public void mouseExited(MouseEvent e){}

}